package com.weiyirong99.crm.app.ui;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.weiyirong99.crm.app.R;
import com.weiyirong99.crm.app.base.BaseFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.TriangularPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

public class PhoneFragment extends BaseFragment {
    private RelativeLayout rlTitle;
    private TextView tvTitle;
    private TextView tvFilet;
    private MagicIndicator magicIndicator;
    private ViewPager viewPager;
    private String title[] = new String[]{"新客户", "新分派", "拓展", "抵押", "锁定", "A类客户", "B类客户", "C类客户", "D类客户", "E类客户"};

    @Override
    public int bindLayout() {
        return R.layout.fragment_phone;
    }

    @Override
    public View initView(View view) {
        rlTitle = (RelativeLayout) view.findViewById(R.id.rl_title);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvFilet = (TextView) view.findViewById(R.id.tv_filet);
        magicIndicator = (MagicIndicator) view.findViewById(R.id.magic_indicator);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        viewPager.setAdapter(new MyAdapter(getFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
        viewPager.setCurrentItem(0);
        initMagicindicator();

        return view;
    }

    private void initMagicindicator() {

        magicIndicator.setBackgroundColor(Color.WHITE);
        //新建导航栏
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
//        commonNavigator.setFollowTouch(true);

//        commonNavigator.setScrollPivotX(0.5f);
        commonNavigator.setEnablePivotScroll(true);
        commonNavigator.setRightPadding(50);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return title == null ? 0 : title.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
//                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
//
//                simplePagerTitleView.setText(title[index]);
//                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
//                simplePagerTitleView.setSelectedColor(Color.parseColor("#000099"));
//
//                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        viewPager.setCurrentItem(index);
//                    }
//                });
//
//                return simplePagerTitleView;
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                commonPagerTitleView.setContentView(R.layout.simple_pager_title_layout);
                final TextView textView = commonPagerTitleView.findViewById(R.id.tv);
//                final ImageView imageView = commonPagerTitleView.findViewById(R.id.iv);
                textView.setText(title[index]);
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {

                    @Override
                    public void onSelected(int index, int totalCount) {
                        textView.setTextColor(getResources().getColor(R.color.blue_500));
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        textView.setTextColor(Color.BLACK);
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
//                        imageView.setScaleX(1.3f + (0.8f - 1.3f) * leavePercent);
//                        imageView.setScaleY(1.3f + (0.8f - 1.3f) * leavePercent);
                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
//                        imageView.setScaleX(0.8f + (1.3f - 0.8f) * enterPercent);
//                        imageView.setScaleY(0.8f + (1.3f - 0.8f) * enterPercent);
                    }


                });

                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("WG", "onClick: " + index);
                        viewPager.setCurrentItem(index);
                    }
                });

                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                TriangularPagerIndicator indicator = new TriangularPagerIndicator(context);
                indicator.setLineColor(Color.parseColor("#2196F3"));
                return indicator;
//                return null;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);

    }

    private class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    Fragment01 fragmentOne = new Fragment01();
                    fragment = fragmentOne;
                    break;
                case 1:
                    Fragment02 fragmentTwo = new Fragment02();
                    fragment = fragmentTwo;
                    break;
                case 2:
                    Fragment03 fragmentThree = new Fragment03();
                    fragment = fragmentThree;
                    break;
                case 3:
                    Fragment04 fragment04 = new Fragment04();
                    fragment = fragment04;
                    break;
                case 4:
                    Fragment05 fragment05 = new Fragment05();
                    fragment = fragment05;
                    break;
                case 5:
                    Fragment06 fragment06 = new Fragment06();
                    fragment = fragment06;
                    break;
                case 6:
                    Fragment07 fragment07 = new Fragment07();
                    fragment = fragment07;
                    break;
                case 7:
                    Fragment08 fragment08 = new Fragment08();
                    fragment = fragment08;
                    break;
                case 8:
                    Fragment09 fragment09 = new Fragment09();
                    fragment = fragment09;
                    break;
                case 9:
                    Fragment10 fragment10 = new Fragment10();
                    fragment = fragment10;
                    break;

//                case 10:
//                    Fragment11 fragment11 = new Fragment11();
//                    fragment = fragment11;
//                    break;

            }

            return fragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }


}